package com.qfedu.dao.impl;

import com.qfedu.dao.CardsDao;

import com.qfedu.pojo.Account;
import com.qfedu.pojo.Cards;
import com.qfedu.pojo.Cards;
import com.qfedu.pojo.History;
import com.qfedu.utils.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.math.BigDecimal;
import java.sql.Connection;
import java.util.List;

public class CardsDaoImpl extends BaseDao implements CardsDao {

    @Override
    public Cards queryCardsByCardnumber(String cardnumber) {
        String sql = "select * from bank_cards where cardnumber = ?";
        return queryForOne(Cards.class, sql,cardnumber);
    }

    @Override
    public int saveCards(Cards cards) {
        String sql = "insert into bank_cards values(?,?,?,?,?)";
        return update(sql,cards.getIdcode(),cards.getName(),cards.getCardnumber(),cards.getMoney(),cards.getBank_name());
    }
/*=======


public class CardsDaoImpl extends BaseDao implements CardsDao {*/
    @Override
    public Cards findCardsByCardNumber(String cardnumber) {
        String sql="select * from bank_cards where cardnumber=?";
        return queryForOne(Cards.class,sql,cardnumber);
    }

    @Override
    public int outMoney(Cards source, BigDecimal money, Connection connection) {
        String sql="update bank_cards set money=? where cardnumber=?";
        return update(connection,sql,money,source.getCardnumber());
    }

    @Override
    public int inMoney(Cards target, BigDecimal money, Connection connection) {
        String sql="update bank_cards set money=? where cardnumber=?";
        return update(connection,sql,money,target.getCardnumber());
    }




    @Override
    public Cards queryCardsByIdcode(String idcode) {
        String sql = "select idcode,name,cardnumber,money,bank_name from bank_cards where idcode = ?";
        return queryForOne(Cards.class,sql,idcode);
    }

    @Override
    public Cards findCardsByIdcode(String idcode) {
        String sql= "";
        return null;
    }

    @Override
    public int updateCards(String money ,String cardnumber) {
        String sql="update bank_cards set money=? where cardnumber=?";
        return update(sql,money,cardnumber);
    }


    /*-----------------------------------------------------------------------*/


    JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());

    @Override
    public Cards findCardsnumber(String s) {
        String sql = "select *from bank_cards where cardnumber = ?";
        List<Cards> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Cards.class), s);
        if(query!=null&&query.size()==1){
            return query.get(0);
        }else {
            return null;
        }
    }

    @Override
    public void inMoney(Cards target, int money, Connection connection) {
        String sql = "update bank_cards set money = money+? where idcode =?";
        try {
            update(connection,sql,money,target.getIdcode());
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public Cards find2(Cards cards) {
        String sql = "SELECT *FROM bank_cards where name =? and cardnumber = ? and bank_name = ?";
        List<Cards> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Cards.class), cards.getName(),cards.getCardnumber(),cards.getBank_name());
        if(query!=null&&query.size()==1){
            return query.get(0);
        }else {
            return null;
        }
    }

    @Override
    public List<Cards> findAllByIdcode(String idcode) {
        String sql = "SELECT *FROM bank_cards where idcode=?";
        return queryForList(Cards.class,sql,idcode);
    }
}
